/**
 * 基本信息标签页相关代码
 */
var clxx = (function() {

	var customerId = qmsk.getQueryValue("customerId");
	var $saveButton = $("<div class='BottomBtn'><input type='button' value='保存信息'></div>");

	var cache = {};
	var id = null;

	function clearInput(id) {
		$("#" + id).find("input[type='text']").val('');
		$("#" + id).find("input[type='checkbox']").val('');
		$("#" + id).find("input[type='radio']").val('');
		$("#" + id).find("select").val('');
		$("#" + id).find("textarea").val('');
	}

	function disabledInput(id) {
		$("#" + id).find("input[type='text']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='checkbox']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='radio']").attr('disabled', 'disabled');
		$("#" + id).find("select").attr('disabled', 'disabled');
		$("#" + id).find("textarea").attr('disabled', 'disabled');
	}

	function removeDisabledInput(id) {
		$("#" + id).find("input[type='text']").removeAttr('disabled');
		$("#" + id).find("input[type='checkbox']").removeAttr('disabled');
		$("#" + id).find("input[type='radio']").removeAttr('disabled');
		$("#" + id).find("select").removeAttr('disabled');
		$("#" + id).find("textarea").removeAttr('disabled');
	}

	/*
	 * 页面加载的时候进行的初始化
	 */
	function loadInit() {
		clearInput("clxx");
		$("#clxx-clxx-edit").unbind();
		$("#clxx-bxxx-edit").unbind();
		$("#clxx-wxxx-edit").unbind();

		disabledInput("clxx");
		$saveButton.remove();

		$saveButton.remove();
		//removeDisabledInput("clxx");
		loadSimpleList();

		$("#clxx_add").unbind('click').bind('click', function() {
			removeDisabledInput("clxx_clxx");
			clearInput("clxx");
			disabledInput("clxx_bxxx");
			disabledInput("clxx_wxxx");
			$saveButton.remove();
			$("#clxx-clxx-edit").unbind();
			$("#clxx-bxxx-edit").unbind();
			$("#clxx-wxxx-edit").unbind();
			$("#clxx_clxx").append($saveButton);
			$saveButton.unbind('click').bind('click', insert);
		});

	}

	function loadSimpleList() {
		qmsk.get('../custCarInfo/find/simpleList/' + customerId, function(response) {
			cache.simpleListResponse = response;
			qmsk.dataTable({
				tbodyId : 'clxx_simpleList',
				heads : ["license", "carBrandName", "carSystemName", "carTypeName", "carAge", "id"],
				response : response,
				sup : ['clxx_simpleList_sup', '共 [index] 条'],
				handler : [[5, detailButton]]
			});
		});

		var detailButton = function(id, row) {
			return "<input  id='car_"+id+"' onclick='_loadCarDetail(" + row + ")' type='button' class='common_btn' value='查看详情'><input onclick='_delCar(" + id + ")' type='button' class='del common_btn' value='删除'>";
		};

	}

	function loadDetail(row) {

		var response = cache.simpleListResponse;

		$saveButton.remove();

		var heads = ["id", "license", "VIN", "engine", "buyDate", "carBrandID", "carBrandName", "carSystemId", "carSystemName", "carTypeId", "carTypeName", "licenseDate", "checkCarDate", "color", "price", "memo"];
		//处理打印数据
		var data = response.getObject(heads, row);
		data.carMemo = data.memo;
		delete data['memo'];

		id = data.id;
		$("#carBrandID").empty().append($("<option value='" + data.carBrandID + "'>" + data.carBrandName + "</option>"));
		$("#carSystemId").empty().append($("<option value='" + data.carSystemId + "'>" + data.carSystemName + "</option>"));
		$("#carTypeId").empty().append($("<option value='" + data.carTypeId + "'>" + data.carTypeName + "</option>"));
		qmsk.bindBean(data);

		loadBxwxDetail(id);

		disabledInput("clxx_clxx");
		disabledInput("clxx_wxxx");
		disabledInput("clxx_bxxx");
		$("#carBrandID").unbind();
		$("#carSystemId").unbind();
		$("#carTypeId").unbind();

		$("#clxx-clxx-edit").unbind('click').bind('click', function() {
			$saveButton.remove();
			$("#clxx_clxx").append($saveButton);
			removeDisabledInput("clxx_clxx");
			$saveButton.unbind('click').bind('click', updateClxx);
		});
		$("#clxx-bxxx-edit").unbind('click').bind('click', function() {
			$saveButton.remove();
			$("#clxx_bxxx").append($saveButton);
			removeDisabledInput("clxx_bxxx");
			$saveButton.unbind('click').bind('click', updateBxxx);
		});
		$("#clxx-wxxx-edit").unbind('click').bind('click', function() {
			$saveButton.remove();
			$("#clxx_wxxx").append($saveButton);
			removeDisabledInput("clxx_wxxx");
			$saveButton.unbind('click').bind('click', updateWxxx);
		});

	}

	function _delCar(row) {
		qmsk.post('../custCarInfo/del' , {id:row}, function(response) {
			artSuccess('删除成功！');
			loadSimpleList();		});
		
	}
	window._delCar=_delCar;
	function delTr(id){
		$("#car_"+id).parents("tr").remove();
	}
	
	function loadBxwxDetail(id) {
		qmsk.get('../custCarInfo/find/bxwxdetail/' + id, function(response) {
			bindBxData(response);
			var url = response.getValue('insuranceFile');
			cache.url = url;
			bindWxData(response);
		});
	}

	//"insuranceFile","insuranceNo","insuranceComId","InsuranceName","policyholder",
	//"InsuranceValidDate","InsuranceEndDate","insuranceTypeId","IndustryTypeName",
	//"RecentlyMileage","RecentlyDate","InsuranceFirstDate","LastRepairDate","LastKeepDate",
	//"NextDate","RepairManName","RepairManTel","RepairManMp","RepairTimes","KeepTimes","factoryTimes"

	function bindBxData(response) {
		var ids = ["insuranceNo", "insuranceComId", "InsuranceName", "policyholder", "insuranceValidDate", "insuranceEndDate", "insuranceTypeId"];
		var data = response.getObject(ids);
		$("#insuranceComId").empty().append("<option value='" + data.insuranceComId + "'>" + data.InsuranceName + "</option>");
		qmsk.bindBean(data);
	}

	function collectionBxData() {
		var ids = ["insuranceNo", "insuranceComId", "policyholder", "insuranceValidDate", "insuranceEndDate", "insuranceTypeId"];
		var data = qmsk.collectionData(ids);
		return data;
	}

	function bindWxData(response) {
		var ids = ["recentlyMileage", "recentlyDate", "insuranceFirstDate", "lastRepairDate", "lastKeepDate", "nextDate", "repairManName", "repairManTel", "repairManMp", "repairTimes", "keepTimes", "factoryTimes"];
		var data = response.getObject(ids);
		qmsk.bindBean(data);
	}

	function collectionWxData() {
		var ids = ["recentlyMileage", "recentlyDate", "insuranceFirstDate", "lastRepairDate", "lastKeepDate", "nextDate", "repairManName", "repairManTel", "repairManMp", "repairTimes", "keepTimes"];
		var data = qmsk.collectionData(ids);
		return data;
	}

	function updateBxxx() {
		var data = collectionBxData();
		data.insuranceFile = cache.url;
		qmsk.post('../custCarInfo/update/bxxx/' + id, data, function(response) {
			artSuccess('更新成功！');
			$saveButton.remove();
			disabledInput("clxx_bxxx");
		});
	}

	function updateWxxx() {
		var data = collectionWxData();
		qmsk.post('../custCarInfo/update/wxxx/' + id, data, function(response) {
			artSuccess('更新成功！');
			$saveButton.remove();
			disabledInput("clxx_wxxx");
		});
	}

	function updateClxx() {

		var heads = ["license", "VIN", "engine", "carTypeId", "buyDate", "licenseDate", "checkCarDate", "color", "price", "carMemo"];
		var data = qmsk.collectionData(heads);

		data.Memo = data.carMemo;

		qmsk.post('../custCarInfo/update/clxx/' + id, data, function(response) {
			artSuccess('更新成功！');
			$saveButton.remove();
			disabledInput("clxx_clxx");
			loadSimpleList();
		});
	}

	/**
	 * 加载品牌信息
	 */
	function loadBrand(self) {
		$(self).blur();
		initSelecter({
			method : 'get',
			url : '../brand/find/all',
			heads : ["code", "name"],
			titles : ["品牌编码", "品牌名称"],
			zindex : 500,
			callBack : function(response, row) {
				var id = response.getValue('id', row);
				var name = response.getValue('name', row);
				var option = $("<option value='" + id + "'>" + name + "</option>");
				$(self).empty().append($(option));
				$("#carSystemId").empty();
				$("#carTypeId").empty();
			}
		});
	}


	window.loadBrand = loadBrand;

	/**
	 * 加载车系
	 */
	function loadCarSystem(self) {

		var brandId = $("#carBrandID").val();
		if (qmsk.isEmpty(brandId)) {
			artAlert('请选择品牌！');
			return;
		}

		$(self).blur();
		initSelecter({
			method : 'get',
			url : '../carSystem/find/all',
			data : {
				brandId : brandId
			},
			heads : ["code", "name"],
			titles : ["车系编码", "车系名称"],
			zindex : 500,
			callBack : function(response, row) {
				var id = response.getValue('id', row);
				var name = response.getValue('name', row);
				var option = $("<option value='" + id + "'>" + name + "</option>");
				$(self).empty().append($(option));
				$("#carTypeId").empty();
			}
		});
	}


	window.loadCarSystem = loadCarSystem;

	/**
	 * 加载车型
	 */
	function loadCarType(self) {
		var carSystemId = $("#carSystemId").val();
		if (qmsk.isEmpty(carSystemId)) {
			artAlert('请选择车辆车系！');
			return;
		}
		initSelecter({
			method : 'get',
			url : '../carType/find/all/',
			data : {
				carSysId : carSystemId
			},
			heads : ["id", "name"],
			titles : ["车型Id", "车型名称"],
			zindex : 500,
			callBack : function(response, row) {
				var id = response.getValue('id', row);
				var name = response.getValue('name', row);
				var option = $("<option value='" + id + "'>" + name + "</option>");
				$(self).empty().append($(option));
			}
		});
	}


	window.loadCarType = loadCarType;

	/**
	 * 加载保险公司
	 */
	function loadInsuranceCom(self) {
		initSelecter({
			method : 'get',
			url : '../insuranceCom/find/opt/',
			heads : ["id", "name"],
			titles : ["公司Id", "公司名称"],
			zindex : 500,
			callBack : function(response, row) {
				var id = response.getValue('id', row);
				var name = response.getValue('name', row);
				var option = $("<option value='" + id + "'>" + name + "</option>");
				$(self).empty().append($(option));
			}
		});
	}

	/**
	 * 加载保险类型
	 */
	function loadCarSystem(self) {

		var brandId = $("#carBrandID").val();
		if (qmsk.isEmpty(brandId)) {
			artAlert('请选择品牌！');
			return;
		}

		$(self).blur();
		initSelecter({
			method : 'get',
			url : '../carSystem/find/all',
			data : {
				brandId : brandId
			},
			heads : ["code", "name"],
			titles : ["车系编码", "车系名称"],
			zindex : 500,
			callBack : function(response, row) {
				var id = response.getValue('id', row);
				var name = response.getValue('name', row);
				var option = $("<option value='" + id + "'>" + name + "</option>");
				$(self).empty().append($(option));
				$("#carTypeId").empty();
			}
		});
	}


	window.loadInsuranceCom = loadInsuranceCom;

	window._loadCarDetail = loadDetail;

	function insert() {

		var heads = ["license", "VIN", "engine", "carTypeId", "buyDate", "licenseDate", "checkCarDate", "color", "price", "carMemo"];
		var data = qmsk.collectionData(heads);

		data.Memo = data.carMemo;

		qmsk.post('../custCarInfo/insert/' + customerId, data, function(response) {
			artSuccess('添加成功！');
			$saveButton.remove();
			loadSimpleList();
		});
	}

	function bdchange() {
		var value = $("#bdfile").val();
		$("#bdfileName").val(value);
	}


	window.bdchange = bdchange;

	function uploadfile() {
		var value = $("#bdfileName").val();
		if (qmsk.isEmpty(value)) {
			artAlert('请先选择文件!');
			return;
		}
		$.ajaxFileUpload({
			url : "../fileUtils/upload",
			secureuri : false,
			fileElementId : "bdfile",
			dataType : "json",
			type : "post",
			success : function(rst, status) {
				if (qmsk.isEmpty(rst)) {
					artError('服务器忙，请联系系统管理员！');
					return;
				}
				var response = new qmsk.Response(rst);
				if (response.state == 0) {
					var url = response.getValue('url');
					cache.url = url;
					artSuccess("上传成功！");
				} else if (response.state == 1) {
					artError(response.msg);
				} else {
					checkStateShowErrorMsgAndDealResult(JSON.stringify(rst));
				}
			}
		});
	}

	function ckbd() {
		var url = cache.url;
		if (qmsk.isEmpty(url)) {
			artAlert('您还没有上传保单！');
			return;
		}
		showFormerImg([url]);
	}


	window.ckbd = ckbd;

	window.uploadfile = uploadfile;

	return {
		init : function() {
			loadInit();
		}
	};

})();
